package Beans;

import gestionnaire.Utilisateur;

import java.io.IOException;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import DAOJPA.DAOUtilisateurJPA;
import outils.Outils;


@ManagedBean
@RequestScoped
public class BeanConnexion implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String password;
    private String message, id;
    private String label1 = "Connexion";
	private String label2 = "Cree un compte";
	private String connexion = "/notInConnexion/Connexion";
	private static boolean Gestion = false;
	
	
	public BeanConnexion ()
	{
		  label1 = "Connexion";
		  label2 = "Cree un compte";
		  connexion = "/notInConnexion/Connexion";

	}
	
	
    
    public String loginProject() {
    	this.password = Outils.getCryptedPassword(password);
        boolean result = DAOUtilisateurJPA.login(id, password);
        if (result) {
            // get Http Session and store username
            HttpSession session = Outils.getSession();
            session.setAttribute("username", id);
           
            Utilisateur u = DAOUtilisateurJPA.getInstance().getUtilisateur(id).get(0);
            if (u.isGestionnaire()) this.Gestion = true ;           	
            
            label1 = "Deconnexion";
            label2 = id ;
            connexion = "/Deconnexion";
                      
            return "home";
        } else {
 
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Invalid Login!","Please Try Again!")); 
            // invalidate session, and redirect to other pages
            //message = "Invalid Login. Please Try Again!";
            return null;
        }
    }
 
    public void logout() throws IOException {
      HttpSession session = Outils.getSession();
      session.invalidate();
      label1 = "Connexion";
	  label2 = "Cree un compte";
	  connexion = "/notInConnexion/Connexion";
	  this.Gestion = false;
	  
	  ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
	  ec.redirect(ec.getRequestContextPath() + "/faces/Home.xhtml"); 
     // return "login";
   }

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLabel1() {
		return label1;
	}

	public void setLabel1(String label1) {
		this.label1 = label1;
	}

	public String getLabel2() {
		return label2;
	}

	public void setLabel2(String label2) {
		this.label2 = label2;
	}



	public String getConnexion() {
		return connexion;
	}



	public void setConnexion(String connexion) {
		this.connexion = connexion;
	}



	public static boolean isGestion() {
		return Gestion;
	}


	public boolean getGestion()
	{
		return this.Gestion;
	}

	public static void setGestion(boolean gestion) {
		Gestion = gestion;
	}




	
	
	
	
	

}
